﻿logger = new Logger("browseTaobao.js");
log = logger.log;
error = logger.error;

var converter = new Converter();

log("Running Taobao Browser  ...  browseTaobao.js")

main();
function main()
{
    var href = location.href;
    if (href.search("item.htm") != -1)
        browseInit();
    else
        msg.send('error', "No Script for this page!");
}
function browseInit()
{
    var scirptsLoaded = false;
    loadScriptAsync("./utilities.js", loadCustomScript);

    function loadCustomScript()
    {
        doWithJQuery(onScirptsLoaded);
    }

    function onScirptsLoaded()
    {
        scirptsLoaded = true;
    }

    var timer = setInterval(checkAllLoaded, 100)
    function checkAllLoaded()
    {
        if (document.readyState === "complete" && scirptsLoaded)
        {
            browseMain();
            clearInterval(timer);
        }
    }
}

function browseMain()
{
    msg.send("itemTabLoaded");
}
var secondsPerScreen = 0.5;
tasks.goTop = function (arg)
{
    secondsPerScreen = arg && arg.secondsPerScreen || secondsPerScreen;
    //description is a id
    var des = $(description);
    var p = des.offset();
    smoothScrollTo(des.offset().top, function ()
    {
        msg.send("topArrived");
    }, secondsPerScreen);
}

tasks.goMid = function ()
{
    var des = $(description);
    smoothScrollTo(des.offset().top + des.height() / 2 - window.innerHeight / 2, function ()
    {
        msg.send("midArrived");
    }, secondsPerScreen);
    //msg.send("midArrived");
}

tasks.goBottom = function ()
{
    var des = $(description);
    smoothScrollTo(des.offset().top + des.height() - window.innerHeight, function ()
    {
        msg.send("bottomArrived");
    }, secondsPerScreen);
}

tasks.goRates = function ()
{
    var des = $("a:contains(累计评论),a:contains(累计评价)");
    smoothScrollTo(des.offset().top, function ()
    {
        des[0].click();
        msg.send("ratesArrived");
    }, secondsPerScreen);
    //des[0].click();
    //msg.send("ratesArrived");
}

tasks.goTrades = function ()
{
    var des = $("a:contains(成交记录)");
    smoothScrollTo(des.offset().top, function ()
    {
        des[0].click();
        msg.send("tradesArrived");
    }, secondsPerScreen);
    //des[0].click();
    //msg.send("tradesArrived");
}

tasks.goCollect = function ()
{
    var jbutton = $("a:contains(收藏商品), a:contains(收藏宝贝)");
    smoothScrollTo(jbutton.offset().top - window.innerHeight / 2, function ()
    {
        jbutton[0].click();
        msg.send("collectItemArrived");
    }, secondsPerScreen);
    //msg.send("collectItemArrived");
}

tasks.goCollectShop = function ()
{
    // TODO : progblem : scroll too fast, object not showing!!!!
    var jbutton = $("span:contains(收藏本店), a:contains(收藏店铺)").last();
    smoothScrollTo(jbutton.offset().top - window.innerHeight / 2, function ()
    {
        jbutton[0].click();
        msg.send("collectShopArrived");
    }, secondsPerScreen);
    //msg.send("collectShopArrived");
}

tasks.getSubItemsLink = function()
{
    var button = $("button#J_ShopSearchIcon, input.J_TSubmitBtn btn:contains(搜索), a:contains(查看所有宝贝), a.show-more:not(a.disappear):contains(查看更多宝贝)").last()[0];
    msg.send("subItemsLink", { link: button.href });
}

tasks.updateSubitemsLink = function ()
{
    var button = $("button#J_ShopSearchIcon, input.J_TSubmitBtn btn:contains(搜索), a:contains(查看所有宝贝), a.show-more:not(a.disappear):contains(查看更多宝贝)").last()[0];
    simulateMouseDownOnObject(button);
    msg.send("subItemsLinkUpdated", { link: button.href });
}

tasks.updateShopIndexLink = function ()
{
    var button = document.querySelector(".tb-shop-name a, a.shopLink");
    simulateMouseDownOnObject(button);
    msg.send("shopIndexLinkUpdated", { link: button.href });
}